package com.hd.crdao.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.base.MyBaseAdapter;
import com.hd.crdao.bean.FindSubAccountBean;
import com.hd.crdao.bean.MsgBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/6/28 0028
 */
public class MyChildAccountActivity extends BaseSecondActivity {
    @BindView(R.id.mychildaccount_head)
    ExpandHeadView mMychildaccountHead;
    @BindView(R.id.iv_cry)
    ImageView mIvCry;
    @BindView(R.id.tv_desc)
    TextView mTvDesc;
    @BindView(R.id.nodata)
    RelativeLayout mNodata;
    @BindView(R.id.childaccountlistview)
    ListView mChildaccountlistview;
    public MyAccountAdapeter mMyAccountAdapeter;
    private Handler mHandler = new Handler();

    @Override
    protected void initView() {
        initData();
    }

    private void initData() {


        OkHttpUtils.post().url(Constants.FINDSUBACCOUNTLIST_URL)
                .addParams("sign", Constants.SIGN)
                .addParams("carBusinessId", SPUtils.getString("businessid", ""))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("无网络连接");
            }

            @Override
            public void onResponse(String response, int id) {
                FindSubAccountBean findSubAccountBean = new Gson().fromJson(response, FindSubAccountBean.class);
                if (findSubAccountBean.getTotal() == 0) {
                    mNodata.setVisibility(View.VISIBLE);
                    mChildaccountlistview.setVisibility(View.GONE);
                } else {
                    mNodata.setVisibility(View.GONE);
                    mChildaccountlistview.setVisibility(View.VISIBLE);
                    List<FindSubAccountBean.RowsBean> rowsBeanList = findSubAccountBean.getRows();
                    FindSubAccountBean.RowsBean rowsBean = new FindSubAccountBean.RowsBean();
                    rowsBean.setName("员工姓名");
                    rowsBean.setMobile("手机号码");
                    rowsBeanList.add(0, rowsBean);
                    mMyAccountAdapeter = new MyAccountAdapeter(rowsBeanList);
                    mChildaccountlistview.setAdapter(mMyAccountAdapeter);
                }
                ALog.d(response);
            }
        });


    }

    @Override
    protected int setContentView() {
        return R.layout.activity_mychildaccount;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mMychildaccountHead;
    }


    private class MyAccountAdapeter extends MyBaseAdapter<FindSubAccountBean.RowsBean> {
        public MyAccountAdapeter(List<FindSubAccountBean.RowsBean> dataSets) {
            super(dataSets);
        }


        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(UIUtils.getContext(), R.layout.item_mychildaccount, null);
                holder.mEployeeName = (TextView) convertView.findViewById(R.id.employeeName);
                holder.mPhoneNumber = (TextView) convertView.findViewById(R.id.phonenumber);
                holder.mUnRegister = (TextView) convertView.findViewById(R.id.unregister);
                holder.mFrameLayout = (FrameLayout) convertView.findViewById(R.id.delete_layout);
                holder.mDelete = (ImageButton) convertView.findViewById(R.id.delete);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            FindSubAccountBean.RowsBean rowsBean = mDataSets.get(position);
            if (position == 0) {
                holder.mFrameLayout.setVisibility(View.GONE);
                holder.mUnRegister.setVisibility(View.VISIBLE);
                holder.mDelete.setVisibility(View.GONE);
            } else {
                holder.mFrameLayout.setVisibility(View.VISIBLE);
                holder.mUnRegister.setVisibility(View.GONE);
                holder.mDelete.setVisibility(View.VISIBLE);
            }
            holder.mEployeeName.setText(rowsBean.getName());
            holder.mPhoneNumber.setText(rowsBean.getMobile());
            holder.mDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ALog.d("删除按钮我点了");
                    final AlertDialog.Builder builder = new AlertDialog.Builder(MyChildAccountActivity.this);
                    builder.setTitle("注销")
                            .setMessage("您确定注销该账户吗?")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    delSubAccount(mDataSets.get(position));
                                }
                            }).setNegativeButton("取消", null).show();


                }
            });
            return convertView;
        }
    }

    private void delSubAccount(FindSubAccountBean.RowsBean rowsBean) {

        OkHttpUtils.post().url(Constants.DELETECARBUSINESSACCOUNT_URL)
                .addParams("sign", Constants.SIGN)
                .addParams("carBusinessId", SPUtils.getString("businessid", ""))
                .addParams("subAccountId", rowsBean.getId())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("无网络连接");
            }

            @Override
            public void onResponse(String response, int id) {
                MsgBean msgBean = new Gson().fromJson(response, MsgBean.class);
                if (msgBean.getSuccess() == 0) {
                    mHandler.postDelayed(new UpDateRunnable(), 1500);

                } else {
                    MyToast.show(msgBean.getMessage());
                }
            }
        });
    }

    class ViewHolder {
        TextView mEployeeName;
        TextView mPhoneNumber;
        TextView mUnRegister;
        FrameLayout mFrameLayout;
        ImageButton mDelete;
    }

    class UpDateRunnable implements Runnable {

        @Override
        public void run() {
            MyToast.show("注销成功");
            initData();
            mMyAccountAdapeter.notifyDataSetChanged();
        }
    }
}
